package com.rxyb.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 交换机类型:fanout(发送到绑定的所有队列中)
 *
 * @author lcf
 * @date 2020-05-25-14:54
 */
@Configuration
public class FanoutExchangeConfig {
    /**
     * 注入Fanout路由策略的Exchange交换机实例
     *
     * @return Exchange“交换机”实例
     */
    @Bean(name = "myFanoutExchange")
    FanoutExchange getFanoutExchange() {
        // 创建并返回名为My-Fanout-Exchange的交换机
        return new FanoutExchange("My-Fanout-Exchange");
    }


    /**
     * 将myFirstQueue对应的队列，绑定到myFanoutExchange对应的交换机
     *
     * @param myFirstQueue
     * @param myFanoutExchange
     */
    @Bean
    Binding bindingQueueOneToFanoutExchange(@Qualifier("myFirstQueue") Queue myFirstQueue,
                                            @Qualifier("myFanoutExchange") FanoutExchange myFanoutExchange) {
        return BindingBuilder.bind(myFirstQueue).to(myFanoutExchange);
    }

    /**
     * 将myTwoQueue对应的队列，绑定到myFanoutExchange对应的交换机
     *
     * @param myTwoQueue
     * @param myFanoutExchange
     */
    @Bean
    Binding bindingQueueTwoToFanoutExchange(@Qualifier("myTwoQueue") Queue myTwoQueue,
                                            @Qualifier("myFanoutExchange") FanoutExchange myFanoutExchange) {
        return BindingBuilder.bind(myTwoQueue).to(myFanoutExchange);
    }

    /**
     * 将myThreeQueue对应的队列，绑定到myFanoutExchange对应的交换机
     *
     * @param myThreeQueue
     * @param myFanoutExchange
     */
    @Bean
    Binding bindingQueueThreeToFanoutExchange(@Qualifier("myThreeQueue") Queue myThreeQueue,
                                              @Qualifier("myFanoutExchange") FanoutExchange myFanoutExchange) {
        return BindingBuilder.bind(myThreeQueue).to(myFanoutExchange);
    }
}
